﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.InfoSys;

namespace Zwms.Client.OutWms
{
    public partial class OutBillWnd : UserControl
    {
        #region 成员变量
        BilOutBill CurrentOutBill { get; set; }
        #endregion

        #region 构造函数
        public OutBillWnd()
        {
            InitializeComponent();
            InitBtnImage();
        }

        public OutBillWnd(BilOutBill outBill)
        {
            InitializeComponent();
            InitBtnImage();
            CurrentOutBill = outBill;
            LoadBill(CurrentOutBill);
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnSelectOutBill.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnSend.Image = global::Zwms.Client.Properties.Resources.list;
            this.btnPrint.Image = global::Zwms.Client.Properties.Resources.print;
            this.btnRejectBill.Image = global::Zwms.Client.Properties.Resources.cancel2;
        }

        private void LoadBill(BilOutBill record)
        {
            tbOutBillCode.Text = record.Id;
            tbOutPlanBillCode.Text = record.OBPlanCode;
            tbPOCode.Text = record.POCode;
            tbBillStatus.Text = CommonDefine.DictOutBillStatus[record.BillStatus];

            ServiceReferenceZwms.ServiceZwmsClient client
             = new ServiceReferenceZwms.ServiceZwmsClient();
            BilOutBillDetail queryRecord = new BilOutBillDetail();
            queryRecord.OBCode = record.Id;
            PagerIndex pagerIndex = new PagerIndex(1, int.MaxValue);
            IList<BilOutBillDetail> outBilDetailList = client.QueryOutBillDetailList(queryRecord, ref pagerIndex);

            this.bilOutBillDetailBindingSource.DataSource = null;
            if (outBilDetailList.Count > 0)
            {
                this.bilOutBillDetailBindingSource.DataSource = outBilDetailList;
            }
        }

        private void Clear()
        {
            this.CurrentOutBill = null;
            this.bilOutBillDetailBindingSource.DataSource = null;
            this.tbOutBillCode.Text = "";
            this.tbOutPlanBillCode.Text = "";
            this.tbPOCode.Text = "";
            this.tbBillStatus.Text = "";
        }
        #endregion

        #region 控件函数
        private void btnSelectOutBill_Click(object sender, EventArgs e)
        {
            QueryOutBillWnd uc = new QueryOutBillWnd();
            ContainerForm window = new ContainerForm(uc, "选择出库计划单");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
                {
                    //无限制
                }
                else if (MainForm.CurrentUser.UserRole.Trim() == "03")
                {
                    //判断是否为该用户的入库计划单
                    if (uc.SelectedRecord.CreateUserCode != MainForm.CurrentUser.Id)
                    {
                        MessageBox.Show("对不起，您不具备该出库单的操作权限！");
                        return;
                    }
                }

                CurrentOutBill = uc.SelectedRecord;
                LoadBill(uc.SelectedRecord);
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (CurrentOutBill == null)
            {
                MessageBox.Show("请首先选择出库单！");
                return;
            }

            if (CurrentOutBill.BillStatus == "2" || CurrentOutBill.BillStatus == "3")
            {
                MessageBox.Show("该出库计划单已完成或撤销！");
                return;
            }

            SendForm window = new SendForm(CurrentOutBill);
            window.ShowDialog();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (CurrentOutBill == null)
            {
                MessageBox.Show("请首先选择订单！");
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client
             = new ServiceReferenceZwms.ServiceZwmsClient();
            BilOutBillDetail queryRecord = new BilOutBillDetail();
            queryRecord.OBCode = CurrentOutBill.Id;
            PagerIndex pagerIndex = new PagerIndex(1, int.MaxValue);
            IList<BilOutBillDetail> outBilDetailList =
                client.QueryOutBillDetailList(queryRecord, ref pagerIndex);
            Report.ReportHelper.PrintOutBill(CurrentOutBill, outBilDetailList.ToList());
        }

        private void btnRejectBill_Click(object sender, EventArgs e)
        {
            if (CurrentOutBill == null)
            {
                MessageBox.Show("请首先选择出库单！");
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            CurrentOutBill.BillStatus = "3";
            if (client.UpdateOutBill(CurrentOutBill))
            {
                SysLogWriter.WriteOptLog("撤销出库单", CurrentOutBill.Id, "改");
                this.Clear();
                MessageBox.Show("成功！");
            }
            else
            {
                MessageBox.Show("失败！");
            }
        }

        #endregion
    }
}
